package com.example.backend.controller;



import com.example.backend.entity.Shop;
import com.example.backend.service.ShopService;
import com.example.backend.util.PwdUtil;
import com.example.backend.util.UUIDUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@Log4j
public class ShopController {
    @Autowired
    private ShopService shopService;

    @PostMapping("/shop")
    public int doAdd(@RequestBody Shop shop){
        log.info("添加商家：" + shop);
        return shopService.addShop(shop);
    }
    @PutMapping("/shop")
    public int doUpdate(@RequestBody Shop shop){
        shop.setPwd(PwdUtil.getEncryptedPwd(shop.getPwd()));
        log.info("修改商家：" + shop);

        return shopService.updateShop(shop);
    }
    @DeleteMapping("/shop/{id}")
    public int doDelete(@PathVariable("id") Integer id){
        log.info("删除商家：" + id);
        return shopService.deleteShop(id);
    }
    @GetMapping("/shop/{id}")
    public Shop doGet(@PathVariable("id") Integer id){
        log.info("查询商家：" + id);
        return shopService.selectShopById(id);
    }
    @GetMapping("/shop")
    public ArrayList<Shop> doList( Shop shop){
        log.info("查询所有商家");
        return shopService.selectAll(shop);
    }
    @GetMapping("/shopGetPage")
    public PageInfo<Shop> shopGetPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            Shop shop) {  // ✅ 接收查询条件
        return shopService.shopGetPage(pageNum, pageSize, shop);
    }


    @PostMapping("/shop/login")
    public Map<String, Object> login(@RequestBody Shop shop) {
        Map<String, Object> result = new HashMap<>();
        try {
            Shop dbShop = shopService.selectByShopname(shop.getShopname());
            if (dbShop != null && PwdUtil.getEncryptedPwd(shop.getPwd()).equals(dbShop.getPwd())) {

                if(dbShop.getChecked().equals("0")){
                    result.put("success", false);
                    result.put("message", "商家审核中");
                }
                else{
                    // 登录成功
                    result.put("success", true);
                    result.put("message", "登录成功");
                    result.put("token", UUIDUtil.getUUID()); // 生成token
                    result.put("shop", dbShop);
                }

            } else {
                result.put("success", false);
                result.put("message", "用户名或密码错误");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统异常");
        }
        return result;
    }

    @PostMapping("/shop/register")
    public Map<String, Object> register(@RequestBody Shop shop) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 检查用户名是否已存在
            Shop dbShop = shopService.selectByShopname(shop.getShopname());
            if (dbShop != null) {
                result.put("success", false);
                result.put("message", "店名已存在");
                return result;
            }

            // 加密密码并保存用户信息
            shop.setPwd(PwdUtil.getEncryptedPwd(shop.getPwd()));
            int count = shopService.addShop(shop);
            if (count > 0) {
                result.put("success", true);
                result.put("message", "注册成功");
            } else {
                result.put("success", false);
                result.put("message", "注册失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统异常");
        }
        return result;
    }

    // 新增方法：获取当前店铺信息
    @GetMapping("/api/shop/current")
    public Map<String, Object> getCurrentShopInfo() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 从请求头中获取 token 或者从本地存储中读取店铺 ID
            String shopId = "default_shop_id"; // 示例值，实际应从请求头或参数中获取
            Shop dbShop = shopService.selectShopById(Integer.parseInt(shopId));
            if (dbShop != null) {
                result.put("success", true);
                result.put("message", "获取成功");
                result.put("data", dbShop);
            } else {
                result.put("success", false);
                result.put("message", "未找到对应店铺信息");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统异常");
        }
        return result;
    }
    @PutMapping("/shop/shopScore/{id}")
    public int updateShopScore(@PathVariable("id") Integer id){
        return shopService.updateShopScore(id);
    }
}
